<?php
class CompaniesController extends AppController{
	
	//var $helpers = Array("Form");
	
	var $uses = Array("Company","City","User","Group");
	
	
	/**
	 * List of all the companies
	 * @return unknown_type
	 */
	public function index(){
		
		//var_dump($this->Acl->Aro);
		
		$this->set('companies',$this->Company->find('all'));
		
	}
	
	/**
	 * Kreiraj kompanija
	 * @return void
	 */
	public function create(){
		
		if(!empty($this->data)){
			
			$user = $this->User->save($this->data);
			
			if(!empty($user)){
				
				$this->data['Company']['user_id'] = $this->User->id;
			
				if($this->Company->save($this->data)){
					
					$this->Acl->Aro->create();
					
					
					$this->flash("Company created","/companies",2);
				}
			}
		}
		
		$cities = $this->City->find('all');
		$cities_array = array();
		
		foreach ($cities as $city){
			$cities_array[$city['City']['id']] = $city['City']['name'];	
		}
		
		$this->set('cities',$cities_array);
		
		$this->set('client_group',$this->Group->find('first',array('conditions' => array('Group.name' => "Clients"))));
		
	}
	
	/**
	 * Detali za kompanija
	 * Lista na objekti, detali za kontakt
	 * @return void
	 */
	public function details(){
		
		if(!empty($this->params['named']['company'])){
			
			$conditions = array("Company.id" => $this->params['named']['company']);
			$comp = $this->Company->find('first',array('conditions' => $conditions));
			$this->set('company',$comp);
			
		}else{
			$this->flash("Ne postoi kompanija so toj ID","/companies",2);
		}
	}
}